package cn.meomis.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class DateUtility {
	
	/**
	 * Is real date format
	 * @param pInput
	 * @return
	 */
	public static boolean isDate (String pInput) { 
        if(pInput == null){ 
            return false; 
        } 
        String regEx = "^((\\d{2}(([02468][048])|([13579][26]))-((((0?[1357" 
                + "8])|(1[02]))-((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])" 
                + "|(11))-((0?[1-9])|([1-2][0-9])|(30)))|(0?2-((0?[1-9])|([1-" 
                + "2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]" 
                + "))[\\-\\/\\s]?((((0?[13578])|(1[02]))-((0?[1-9])|([1-2][0-" 
                + "9])|(3[01])))|(((0?[469])|(11))-((0?[1-9])|([1-2][0-9])|(3" 
                + "0)))|(0?2-((0?[1-9])|(1[0-9])|(2[0-8]))))))$"; 
        Pattern p = Pattern.compile(regEx); 
        Matcher matcher = p.matcher(pInput); 
        return matcher.matches(); 
    }
	
	/**
	 * Compare two date, return the num of days from sDate2 to sDate1 and value is absolute value
	 * 
	 * @return int
	 * @param sDate1
	 *            java.lang.String
	 * @param sDate2
	 *            java.lang.String
	 */
	public static int compareDate(String sDate1, String sDate2) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = dateFormat.parse(sDate1);
			date2 = dateFormat.parse(sDate2);
		} catch (ParseException e) {
			throw new IllegalArgumentException(e.getMessage());
		}

		long dif = 0;
		if (date2.after(date1))
			dif = (date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24;
		else
			dif = (date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24;

		return (int) dif;
	}

	/**
	 * Compare two date, return the num of days from sDate2 to sDate1
	 * 
	 * @return int
	 * @param sDate1
	 *            java.lang.String
	 * @param sDate2
	 *            java.lang.String
	 */
	public static int compareRealDate(String sDate1, String sDate2) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = dateFormat.parse(sDate1);
			date2 = dateFormat.parse(sDate2);
		} catch (ParseException e) {
			throw new IllegalArgumentException(e.getMessage());
		}

		long dif = (date2.getTime() - date1.getTime()) / 1000L / 60L / 60L / 24L;

		return (int) dif;
	}
	
	/**
	 * Compare the date input and current date, return the num of days from current date to sDate
	 * @param sDate
	 * @return
	 */
	public static int compareCurrentDate(String sDate){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = null;
		try {
			date1 = dateFormat.parse(sDate);
		} catch (ParseException e) {
			throw new IllegalArgumentException(e.getMessage());
		}

		long dif = (System.currentTimeMillis() - date1.getTime()) / 1000L / 60L / 60L / 24L;

		return (int) dif;
	}

	/**
	 * Get the date string from current date
	 * @param dayNum
	 * @return
	 */
	public static String getNextDate(int dayNum) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, dayNum);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String s = df.format(calendar.getTime());
		return s;
	}

	/**
	 * Get the next year date string based on the date input
	 * @param java.lang.String
	 *            date format is YYYY-MM-DD
	 * @return java.lang.String
	 */
	public static String getNextYearDate(String date) {
		String rn = date;
		try {
			if (date != null) {
				String[] dateArray = date.split("-");
				int year = Integer.parseInt(dateArray[0]);
				year++;
				rn = String.valueOf(year) + "-" + dateArray[1] + "-"
						+ dateArray[2];
			}
		} catch (Exception e) {
			rn = date;
		}
		return rn;
	}

	/**
	 * Get current date string format is YYYY-MM-DD
	 * 
	 * @return java.lang.String
	 */
	public static String getCurrentDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String s = df.format(new Date());
		return s;
	}

	/**
	 * Get current datetime format is YYYY-MM-DD HH:mm:ss
	 * 
	 * @return java.lang.String
	 */
	public static String getCurrentDateTime() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = df.format(new Date());
		return s;
	}

	/**
	 * Get current time format is HH:mm:ss
	 * 
	 * @return java.lang.String
	 */
	public static String getCurrentTime() {
		SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
		return df.format(new Date());
	}

	/**
	 * Get current day
	 * 
	 * @return java.lang.String
	 */
	public static String getCurrentDay() {
		String day;
		SimpleDateFormat df = new SimpleDateFormat("dd");
		day = df.format(new Date());
		return day;
	}

	/**
	 * Get current month
	 * 
	 * @return java.lang.String
	 */
	public static String getCurrentMonth() {
		String month;
		SimpleDateFormat df = new SimpleDateFormat("MM");
		month = df.format(new Date());
		return month;
	}

	/**
	 * Get current year
	 * 
	 * @return java.lang.String
	 */
	public static String getCurrentYear() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy");
		return df.format(new Date());
	}

	/**
	 * Change string date to long
	 * 
	 * @param strDate
	 *            type:YYYY-MM-DD
	 * @return java.lang.Long
	 */
	public static Long getLongDate(String strDate) {
		Date date = java.sql.Date.valueOf(strDate);
		Long lDate = new Long(date.getTime());
		return (lDate);
	}

	/**
	 * Change date string formated by iType to long
	 * 
	 * @param iType
	 *            value strDate type 0 :YYYY-MM-DD 1 :YYYY-MM-DD hh:mm:ss
	 * @return java.lang.Long
	 */
	public static Long getLongDate(String strDate, int iType) {
		Long retDate = null;
		switch (iType) {
		case 0:
			retDate = getLongDate(strDate);
			break;
		case 1:
			retDate = new Long(java.sql.Timestamp.valueOf(strDate).getTime());
			break;
		}
		return retDate;
	}

	/**
	 * Change long date to string date formated by iType
	 * 
	 * @param lDate
	 *            iType value output 0 YYYY-MM-DD 1 YYYY-MM-DD hh 2 YYYY-MM-DD
	 *            hh:ss 3 YYYY-MM-DD hh:ss:mm
	 * @return java.lang.String
	 */
	public static String getStrDate(long lDate, int iType) {
		Date date = new Date(lDate);
		SimpleDateFormat simpleDateFormat = null;
		switch (iType) {
		case 0:
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			break;
		case 1:
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
			break;
		case 2:
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			break;
		case 3:
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		default:
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}

		String strDate = simpleDateFormat.format(date);
		return (strDate);
	}

	/**
	 * Compute string date by sDate input and inteval
	 * @param sDate
	 * @param interval
	 * @return
	 * @author kxcheng
	 * @date 2005-02-25
	 */
	public static String computeStrDate(Date sDate, long interval) {
		long m = 1000;
		long x = sDate.getTime() + Long.parseLong(interval + "") * m;
		SimpleDateFormat simpleDateFormat = null;
		simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date nDate = new Date(x);
		String strDate = simpleDateFormat.format(nDate);
		return strDate;
	}

	/**
	 * Compare sDate1 and sDate2
	 * 
	 * @return boolean sDate1 < sDate2 return true
	 * @param sDate1
	 *            java.lang.String
	 * @param sDate2
	 *            java.lang.String
	 * @author kxcheng
	 * @date 2005-02-27
	 */
	public static boolean compareDateTime(String sDate1, String sDate2) {
		Long sDateTime1 = getLongDate(sDate1, 1);
		Long sDateTime2 = getLongDate(sDate2, 1);
		int compare = sDateTime1.compareTo(sDateTime2);
		if (compare < 0)
			return true;
		else
			return false;
	}

	/**
	 * Compare sDate1 and sDate2
	 * 
	 * @return int sDate1 - sDate2 return second
	 * @param sDate1
	 *            java.lang.String
	 * @param sDate2
	 *            java.lang.String
	 * @author kxcheng
	 * @date 2005-02-27
	 */
	public static int compareTime(String sDate1, String sDate2) {
		Long sDateTime1 = getLongDate(sDate1, 1);
		Long sDateTime2 = getLongDate(sDate2, 1);
		int compare = sDateTime1.intValue() - sDateTime2.intValue();
		return compare / 1000;
	}

	public static int getCurDayOfYear() {
		return getCurDayOfYear(getCurrentDate());
	}

	public static int getCurDayOfYear(String dataStr) {
		return compareDate(dataStr, dataStr.substring(0, 4) + "-01-01");
	}
	
	public static int getCurDayOfWeek(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date(System.currentTimeMillis()));
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if(dayOfWeek < 0){
			dayOfWeek = 0;
		}
		return dayOfWeek;
	}

	public static String getCurWeekOfYear() {
		return getWeekOfYear(getCurrentDate());
	}

	public static String getWeekOfYear(String dateStr) {
		int day = getCurDayOfYear(dateStr);
		String firstDay = dateStr.substring(0, 5) + "01-01";
		SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");

		Calendar calendar = Calendar.getInstance();
		Date date = new Date();

		try {
			date = sdfInput.parse(firstDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		calendar.setTime(date);
		int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

		return dateStr.substring(0, 5) + (day + firstDayOfWeek + 6 - 1) / 7;
	}

}
